package com.yanghui.lingyue.utils.functionUtils

import com.icedata.sika.bridge.*
import com.yanghui.lingyue.bot.Bot
import java.util.*

object FunctionUtils {

    fun atBot(event: SikaGroupEvent): Boolean {
        if (!event.message.hasAtMessage)
            return false
        for (m in event.message) {
            if (m is SikaAtMessage) {
                if (m.target == Bot.sikaCore.config.qq)
                    return true
            }
        }
        return false
    }

    inline fun <reified T : SikaSingleMessage> extractSingleMessage(event: SikaGroupEvent): LinkedList<T> {
        val result = LinkedList<T>()
        for (m in event.message) {
            if (m is T) {
                result.add(m)
            }
        }
        return result
    }

    fun extractKeyword(event: SikaGroupEvent, keyword: String): String? {
        val messageContent = extractPlainMessage(event)
        val param = messageContent.replace(keyword, "")
        if (messageContent.contains(keyword) and param.isNotEmpty()) {
            return param.trim()
        }
        if (messageContent.contains(keyword)) {
            return ""
        }
        return null
    }

    fun extractPlainMessage(event: SikaGroupEvent): String {
        var messageContent = ""
        extractSingleMessage<SikaPlainMessage>(event).forEach {
            messageContent += it.contentToString()
        }
        return messageContent.trim()
    }

    fun extractAtMessage(event: SikaGroupEvent): LinkedList<SikaAtMessage> {
        return extractSingleMessage(event)
    }

    fun extractQuoteMessage(event: SikaGroupEvent): SikaQuoteMessage? {
        for (m in event.message) {
            if (m is SikaQuoteMessage) {
                return m
            }
        }
        return null
    }

    fun checkValid(event: SikaGroupEvent): Boolean {
        if (Bot.groupList[event.sendGroup.id]?.groupCache?.get("OnActive") == true) {
            return true
        }
        return false
    }
}